package com.jiangnan.controller;

import com.jiangnan.service.IFileService;
import com.jiangnan.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

/**
 *
 * @author chenliang
 * @email wschenliang@aliyun.com
 */
@RestController
@RequestMapping("file")
public class FileController {

    @Autowired
    private IFileService fileService;

    @GetMapping("ping")
    public String ping(){
        return "success!";
    }


    @PostMapping("fileUpload")
    public ResponseEntity fileUpload(@RequestParam("file") MultipartFile file) throws Exception {
        String filename = file.getOriginalFilename();
        if (StringUtils.isBlank(filename)) {
            return new ResponseEntity(HttpStatus.BAD_REQUEST);
        }
        byte[] fileBytes = file.getBytes();
        byte[] excel = fileService.convertJson2Excel(fileBytes);
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
        headers.setContentDispositionFormData("attachment", "outVec.xlsx");
        return  new ResponseEntity(excel, headers, HttpStatus.OK);
    }



}
